<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 All Rights Reserved.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Repositories;


use App\Models\Cate;
use App\Models\Song;
use Illuminate\Http\Request;

/**
 * Class SongRepository
 * @package App\Repositories
 */
class SongRepository extends BaseRepository
{
    public function __construct(Song $model)
    {
        $this->model = $model;
    }

    /**
     * @inheritdoc
     */
    public function rules($model)
    {
        return [
            'name' => 'required',
        ];
    }

    /**
     * @inheritdoc
     */
    protected function buildQuery(Request $request)
    {
        $query = parent::buildQuery($request);

        if ($q = $request->query('q', false)) {
            $query->where('name', 'like', '%' . $q . '%');
        }

        return $query;
    }

    /**
     * @inheritdoc
     */
    protected function prepareReceptionStemQuery(Request $request)
    {
        $query = parent::prepareReceptionStemQuery($request);
        return $query->where('status', Cate::STATUS_ONLINE);
    }

    /**
     * 根据歌曲id查询
     *
     * @param $sing_id
     * @param Request|null $request
     * @return \App\Models\BaseModel|\Illuminate\Database\Eloquent\Builder
     */
    public function findBySongId($sing_id, Request $request = null)
    {
        return $this->prepareModelQuery($this->getRequest($request))->where('song_id', '=', $sing_id)->firstOrFail();
    }
}
